const { ccclass, property } = cc._decorator;
declare let eManager,dbh,DBManager,i18n,JsonHttp;
import BaseView from "../../component/BaseView";
import HzMiniSelectIconList from "./HzMiniSelectIconList";
import UIHelper = require("../../component/UIHelper");

import DrawOne from "../DrawOne";
@ccclass
export default class RepairEarFourView extends BaseView {
    @property(cc.Node)
    bg: cc.Node = null;
    @property(HzMiniSelectIconList)
    selectList: HzMiniSelectIconList = null;
    
    @property(DrawOne)
    earDraw: DrawOne = null;
    @property(sp.Skeleton)
    pointSpine2: sp.Skeleton = null;
    @property(sp.Skeleton)
    pointSpine3: sp.Skeleton = null;
    @property(cc.Node)
    guideNode1: cc.Node = null;
    @property(cc.Node)
    guideNode2: cc.Node = null;
    @property(cc.Node)
    guideNode3: cc.Node = null;

    @property(cc.Node)
    endNode: cc.Node = null;
    @property(cc.Node)
    btnClose: cc.Node = null;

    _point = 1;
    _isPlay = false;
    _isDone = false;
    _Step = 1;
    _StepNum = 4;
    _isStart = false;//是否是剧情前触发
    onLoad() {
        super.onLoad();
        this.bg.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.bg.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.bg.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        //@ts-ignore
        this._isStart = this.node.openParam;
    };

    onClose(){
    };
    start(){
        super.start();
        this.btnClose.active = !this._isStart;
    };

    onTouchStart(event) {
        if(this._isDone)
            return;
        if(this._isPlay)
            return;
        switch (this._point) {
            case 1:
                this.earDraw._touchStart(event,cc.v2(-100,100));
                break;
            case 5:
                let state = this.pointSpine3.getState();
                if(state){
                    if(this.pointSpine3.paused == true){
                        this.pointSpine3.paused = false;
                    }else{
                        this.pointSpine3.setAnimation(0,"01",false);
                        this.pointSpine3.setCompleteListener(()=>{
                            this._isDone = true;
                            this.endNode.active = true;
                        });
                    }
                }
                break;
            default:
                break;
        }

    };
    onTouchMove(event) {
        if(this._isDone)
            return;
        if(this._isPlay)
            return;
        switch (this._point) {
            case 1:
                this.earDraw._touchMove(event);
                break;
            default:
                break;
        }
    };
    onTouchEnd(event) {
        if(this._isDone)
            return;
        if(this._isPlay)
            return;
        switch (this._point) {
            case 1:
                this.earDraw._touchEnd(() => {
                    this._point ++;
                    this.earDraw.node.parent.active = false;
                    this.pointSpine2.node.parent.active = true;
                    this.guideNode1.active = true;
                });
                break;
            case 5:
                this.pointSpine3.paused = true;
                break;
            default:
                break;
        }
    };

    onClickGuide(){
        if(this._point == 2){
            this.guideNode1.active = false;
            this._isPlay = true;
            this.pointSpine2.setAnimation(0,"01",false);
            this.pointSpine2.setCompleteListener(()=>{
                this._point ++;
                this.guideNode2.active = true;
                this._isPlay = false;
            });
        }else if(this._point == 3){
            this.guideNode2.active = false;
            this._isPlay = true;
            this.pointSpine2.setAnimation(0,"02",false);
            this.pointSpine2.setCompleteListener(()=>{
                this._point ++;
                this._isPlay = false;
                this.guideNode3.active = true;
            });
        }else if(this._point == 4){
            this.guideNode3.active = false;
            this._isPlay = true;
            this.pointSpine2.setAnimation(0,"03",false);
            this.pointSpine2.setCompleteListener(()=>{
                this._point ++;
                this._isPlay = false;
                this.pointSpine3.node.parent.active = true;
                this.pointSpine2.node.parent.active = false;
                // this.pointSpine2.setAnimation(0,"04",false);
                // this.pointSpine2.setCompleteListener(()=>{
                    
                // });
            });
        }
    };

    onClickEnd(isSkip){
        let next = this.params.next && this.params.next.shift();
        if (next) {
            UIHelper.openUI(next, {
                next: this.params.next
            })
            this.node.destroy();
        }
    };

    onClickSkip(){
        this.onClickEnd(true);
    };
}
